package com.controller.custom;

import com.service.EmployeeServiceImpl;
import com.utils.Pagination;
import entity.EmpEntity;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/custom")
public class CustomPageServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 让客户端浏览器区别不同种类的数据
        resp.setContentType("application/json;charset=UTF-8");

        String pageNumString = req.getParameter("pagenum");
        String pageSizeString = req.getParameter("pagesize");

        int pageNum = pageNumString == null || pageNumString.isEmpty()
                ? 1 : Integer.parseInt(pageNumString);
        int pageSize= pageSizeString == null || pageSizeString.isEmpty()
                ? 3 : Integer.parseInt(pageSizeString);

        EmployeeServiceImpl employeeService = new EmployeeServiceImpl();
        List<EmpEntity> list = employeeService.getAllByCustom(pageNum, pageSize);

        int recordCount = employeeService.getCountByCustom();
        Pagination pagination = new Pagination(pageNum, recordCount, pageSize);

        req.setAttribute("page", pagination);
        req.setAttribute("list", list);
        req.getRequestDispatcher("/WEB-INF/custom.jsp").forward(req, resp);
    }
}
